package com.example.huwang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.huwang.R;
import com.example.huwang.adapter.CaseAdapter;
import com.example.huwang.adapter.IconAdapter;
import com.example.huwang.model.CaseBean;
import com.example.huwang.model.DefinitionBean;
import com.example.huwang.model.Icon;
import com.example.huwang.utils.AppConfig;
import com.example.huwang.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.huwang.app.MainApplication.dbController;

public class MainActivity extends BaseActivity {
    @BindView(R.id.search_btn)
    public ImageView searchBtn;
    @BindView(R.id.grid_photo)
    public GridView gridPhoto;
    @BindView(R.id.recyclerview)
    public RecyclerView recyclerview;
    @BindView(R.id.icon_user)
    public ImageView iconUser;
    @BindView(R.id.search_txt)
    public EditText searchTxt;


    private ArrayList<Icon> mData = null;
    private BaseAdapter mAdapter = null;
    private CaseAdapter caseAdapter;
    private List<CaseBean> caseBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定初始化ButterKnife
        ButterKnife.bind(this);
        initFirst();
        initData();
        initview();
    }

    private void initview() {
        iconUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, UserInfoActivity.class);
                startActivity(intent);
            }
        });
        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Toast.makeText(MainActivity.this, "搜索", Toast.LENGTH_SHORT).show();
                searchList();
            }
        });
        mAdapter = new IconAdapter(this, mData);
        gridPhoto.setAdapter(mAdapter);
        gridPhoto.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(MainActivity.this, "你点击了~" + position + "~项", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, CrimeTypeDetailActivity.class);
                intent.putExtra("type", mData.get(position).getType());
                MainActivity.this.startActivity(intent);
            }
        });

        //案例
        //设置LayoutManager，以LinearLayoutManager为例子进行线性布局
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        //创建适配器
        caseAdapter = new CaseAdapter(caseBeanList);
        //设置适配器
        recyclerview.setAdapter(caseAdapter);
        caseAdapter.setOnClikListener(new CaseAdapter.OnClikListener() {
            @Override
            public void onClick(View view, int position) {
//                Toast.makeText(MainActivity.this, "查看更多", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, MoreDetailActivity.class);
                intent.putExtra("title",caseBeanList.get(position).getTitle());
                intent.putExtra("content",caseBeanList.get(position).getContent());
                Long pic_num = Long.valueOf(position);
                intent.putExtra("pic_num",pic_num);
                startActivity(intent);
            }
        });
    }

    private void initData() {
        mData = new ArrayList<>();
        mData.add(new Icon(R.mipmap.icon_1, 1, "网购诈骗"));
        mData.add(new Icon(R.mipmap.icon_2, 2, "游戏诈骗"));
        mData.add(new Icon(R.mipmap.icon_3, 3, "盗号诈骗"));
        mData.add(new Icon(R.mipmap.icon_4, 4, "刷单诈骗"));
        mData.add(new Icon(R.mipmap.icon_5, 5, "虚假中奖"));
        mData.add(new Icon(R.mipmap.icon_6, 6, "校园贷"));

        caseBeanList = new ArrayList<>();

        List<CaseBean> data = dbController.getCaseBeanDao().queryBuilder().build().list();
        if (data != null && !data.isEmpty()) {
            caseBeanList.clear();
            caseBeanList.addAll(data);
        }
//        caseBeanList.add(new CaseBean("谨防刷单诈骗", "谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗"));
//        caseBeanList.add(new CaseBean("谨防购物诈骗", "谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗"));
//        caseBeanList.add(new CaseBean("谨防游戏诈骗", "谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗"));
//        caseBeanList.add(new CaseBean("谨防盗号诈骗", "谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗谨防刷单诈骗"));
    }

    private void initFirst() {
        if (SPUtils.getInstance().isFirstOpen()) {
            /**
             * 预置诈骗类型列表
             */
            List<DefinitionBean> definitionConfig = AppConfig.getDefinitionConfig();
            dbController.getDefinitionBeanDao().insertOrReplaceInTx(definitionConfig);
            /**
             * 预置案例
             * 通过assets/content_config.json进行配置
             */
            List<CaseBean> contentConfig = AppConfig.getContentConfig();
            dbController.getCaseBeanDao().insertOrReplaceInTx(contentConfig);
            SPUtils.getInstance().setIsFirstOpen(false);
        }
    }

    private void searchList(){
//        dbController.searchByWhere(searchTxt.getText().toString());
        caseBeanList.clear();
        caseBeanList.addAll(dbController.searchCaseByWhere(searchTxt.getText().toString()));
        caseAdapter.notifyDataSetChanged();
    }

}